﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Core.ManageUser;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels.WorkforceManage;
using VOL.SmartMecial.Dtos;
using VOL.SmartMecial.Infrastructure;
using VOL.SmartMecial.IServices.WorkforceManage;

namespace VOL.SmartMecial.Services.WorkforceManage
{
    public class WorkforceService : IWorkforceService
    {
        private readonly IBaseRepository<WorkforceModel> _workforcrepository;
        private readonly IBaseRepository<Nurse> _nurseRepository;

        public WorkforceService(IBaseRepository<WorkforceModel> workforcrepository, IBaseRepository<Nurse> nurseRepo)
        {
            this._workforcrepository = workforcrepository;
            this._nurseRepository = nurseRepo;
        }

        public async Task<WebResponseContent> AddWorkforeces(List<WorkforceModel> list)
        {
            var response = new WebResponseContent();
            var lastDay = list.Last().Stage;
            var tempEntity = _workforcrepository.GetAll().Where(o => o.NurseCode == list[0].NurseCode && o.Year == list[0].Year && o.Month == list[0].Month && o.Stage == lastDay);
            if (tempEntity.Any())
            {
                response.Status = false;
                response.Message = "添加失败,当前阶段已排班";
                return await Task.FromResult(response);
            }
            var creator = UserContext.Current.UserName;
            var time = DateTime.Now;
            list.ForEach(o =>
            {
                o.Creator = creator;
                o.CreateTime = time;
            });
            int c = await _workforcrepository.AddRange(list);
            if (c > 0)
            {
                response.OK("添加成功", c);
            }
            response.OK("添加成功");
            return await Task.FromResult(response);
        }

        public async Task<WebResponseContent> GetNurseList()
        {
            var response = new WebResponseContent();
            var list = await _nurseRepository.GetAll().ToListAsync();
            response.OK("查询成功", list);
            return await Task.FromResult(response);
        }

        public async Task<WebResponseContent> GetWorkforces(int year, int month, int stage)
        {
            var response = new WebResponseContent();
            var list = _workforcrepository.GetAll().Where(o => o.Year == year && o.Month == month);
            if (stage == 1)
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) <= 15);
            }
            else
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) > 15);
            }
            response.OK("查询成功", list);
            return await Task.FromResult(response);
        }

        public async Task<WebResponseContent> GetWorkforcesDetail(GetWorkforceDetailDto obj)
        {
            var list = _workforcrepository.GetAll()
                .Where(o => o.NurseCode == obj.nurseCode && o.Year == obj.year && o.Month == obj.month);

            if (obj.stage == 1)
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) <= 15);
            }
            else
            {
                list = list.Where(o => Convert.ToInt32(o.Stage) > 15);
            }
            var response = new WebResponseContent();
            response.OK("查询成功", list);
            return await Task.FromResult(response);
        }

        public async Task<WebResponseContent> UpdWorkforeces(List<WorkforceModel> list)
        {
            var response = new WebResponseContent();
            var creator = UserContext.Current.UserName;
            var time = DateTime.Now;
            list.ForEach(o =>
            {
                o.Creator = creator;
                o.CreateTime = time;
            });
            int c = await _workforcrepository.UpdRange(list);
            if (c > 0)
            {
                response.OK("修改成功", c);
            }
            return await Task.FromResult(response);
        }
    }
}
